base64-arraybuffer.js ➔ decode   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
dl 0
loc 31
rs 8.9833
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2019 Rafael da Silva Rocha.
3
 * Copyright (c) 2017 Brett Zamir, 2012 Niklas von Hertzen
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining
6
 * a copy of this software and associated documentation files (the
7
 * "Software"), to deal in the Software without restriction, including
8
 * without limitation the rights to use, copy, modify, merge, publish,
9
 * distribute, sublicense, and/or sell copies of the Software, and to
10
 * permit persons to whom the Software is furnished to do so, subject to
11
 * the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
 *
24
 */
25
26
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
27
28
/**
29
 * Encode a byte buffer as a base64 string.
30
 * @param {!Uint8Array} bytes The buffer.
31
 * @return {string} A .wav file as a DataURI.
32
 */
33
export function encode(bytes) {
34
  /** @type {string} */
35
  let base64 = '';
36
  for (let i = 0; i < bytes.length; i += 3) {
37
    base64 += chars[bytes[i] >> 2];
38
    base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
39
    base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
40
    base64 += chars[bytes[i + 2] & 63];
41
  }
42
  if (bytes.length % 3 === 2) {
43
    base64 = base64.substring(0, base64.length - 1) + '=';
44
  } else if (bytes.length % 3 === 1) {
45
    base64 = base64.substring(0, base64.length - 2) + '==';
46
  }
47
  return base64;
48
}
49
50
/**
51
 * Decode a base64 string as a byte as buffer.
52
 * @param {string} base64 A .wav file as a DataURI.
53
 * @return {!Uint8Array} A .wav file as a DataURI.
54
 */
55
export function decode(base64) {
56
  /** @type {!Uint8Array} */
57
  let lookup = new Uint8Array(256);
58
  for (let i = 0; i < chars.length; i++) {
59
    lookup[chars.charCodeAt(i)] = i;
60
  }
61
  /** @type {number} */
62
  let bufferLength = base64.length * 0.75;
63
  if (base64[base64.length - 1] === '=') {
64
    bufferLength--;
65
    if (base64[base64.length - 2] === '=') {
66
      bufferLength--;
67
    }
68
  }
69
  /** @type {!Uint8Array} */
70
  let bytes = new Uint8Array(bufferLength);
71
  for (let i = 0, j = 0; i < base64.length; i += 4) {
72
    /** @type {number} */
73
    let encoded1 = lookup[base64.charCodeAt(i)];
74
    /** @type {number} */
75
    let encoded2 = lookup[base64.charCodeAt(i + 1)];
76
    /** @type {number} */
77
    let encoded3 = lookup[base64.charCodeAt(i + 2)];
78
    /** @type {number} */
79
    let encoded4 = lookup[base64.charCodeAt(i + 3)];
80
    bytes[j++] = encoded1 << 2 | encoded2 >> 4;
81
    bytes[j++] = (encoded2 & 15) << 4 | encoded3 >> 2;
82
    bytes[j++] = (encoded3 & 3) << 6 | encoded4 & 63;
83
  }
84
  return bytes;
85
}
86